//
//  BHMePersonalItemGroupView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/6/11.
//

import UIKit

class BHMePersonalItemGroupView: UIView {
    // 点击哪个
    var toolsDidSelectIndex:((Int,BHMeGroupItemModel) -> Void)?
    
    var minSpace: CGFloat = 15 {
        didSet {
            self.flowLayout.minimumInteritemSpacing = minSpace
        }
    }
    
    let minLineSpace: Int = 15
    
    var companyType: Int = 0 {
        didSet {
            self.collectionView.reloadData()
        }
    }
    
    lazy var title_label: UILabel = {
        let label = UILabel.init()
        label.text = "我的"
        label.textColor = .color_333333
        label.font = .systemFont(ofSize: 15, weight: .bold)
        return label
    }()
    
    
    lazy var flowLayout: UICollectionViewFlowLayout = {
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout.init()
        layout.sectionInset = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
        layout.minimumLineSpacing = CGFloat(self.minLineSpace)
        return layout
    }()
    
    lazy var collectionView: UICollectionView = {
        let view = UICollectionView.buildCollectionView(layout: self.flowLayout)
        view.delegate = self
        view.dataSource = self
        view.register(UINib.init(nibName: "BHMeGroupItemCell", bundle: nil), forCellWithReuseIdentifier: "BHMeGroupItemCell")
        view.bounces = false
        return view
    }()
    
    lazy var contentView: UIView = {
        let view = UIView.init()
        view.backgroundColor = .white
        return view
    }()
    
    var itemArray: [BHMeGroupItemModel] = [] {
        didSet {
            self.collectionView.reloadData()
        }
    }
    
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .color_f8f8f8
        self.itemArray = BHMeGroupItemModel.groupToolsArray()
        self.rederSecRect()
        self.addSubview(self.contentView)
        self.contentView.addSubview(self.title_label)
        self.contentView.addSubview(self.collectionView)
        

        self.clipsToBounds = true
    }
    
    func rederSecRect() {
        
        let space:CGFloat = 74
        let line = self.itemArray.count / 4
        let last = self.itemArray.count % 4
        
        
        var viewheight: CGFloat = 0.0
        if last == 0 {
            if line == 0 {
                viewheight = 85+space
            }else {
                viewheight = CGFloat((60+self.minLineSpace)*(line-1))+85+space
            }
        }else {
            viewheight = CGFloat((60+self.minLineSpace)*line)+85+space
        }
        
        self.frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: viewheight)
        self.layoutFrame()
    }
    
    func layoutFrame() {
        self.contentView.frame = CGRect(x: 15, y: 10, width: UIScreen.width-30, height: self.bh_height-20)
        self.contentView.addCorner(conrners: .allCorners, radius: 8)
        self.title_label.frame = CGRect(x: 25, y: 15, width: 200, height: 18)
        
        self.collectionView.frame = CGRect(x: 0, y: 54, width: UIScreen.width-30, height: self.bh_height-54)
    }

    var viewHeight: CGFloat  {
        get {
            let space:CGFloat = 74
            let line = self.itemArray.count / 4
            let last = self.itemArray.count % 4
            
            
            var viewheight: CGFloat = 0.0
            if last == 0 {
                if line == 0 {
                    viewheight = 85+space
                }else {
                    viewheight = CGFloat((60+self.minLineSpace)*(line-1))+85+space
                }
            }else {
                viewheight = CGFloat((60+self.minLineSpace)*line)+85+space
            }
            return viewheight
        }
    }
    
}

extension BHMePersonalItemGroupView: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.itemArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width_ = (UIScreen.width-11-3*self.minSpace-30)/4.0
        let last = self.itemArray.count % 4

        if indexPath.row < self.itemArray.count - last {
            return CGSize(width: width_, height: 60)
        }else {
            return CGSize(width: width_, height: 85)
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let itemCell = collectionView.dequeueReusableCell(withReuseIdentifier: "BHMeGroupItemCell", for: indexPath) as! BHMeGroupItemCell
        let model: BHMeGroupItemModel = self.itemArray[indexPath.row]
        itemCell.model = model
        return itemCell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let model: BHMeGroupItemModel = self.itemArray[indexPath.row]
        self.toolsDidSelectIndex?(indexPath.row,model)
    }
   

}
